package org.visionlibrary.image.filters.effects;

import java.awt.Point;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.model.WindowFilter;


public class Oil extends WindowFilter {
	protected int width = 3;

	protected int height = 3;

	public Oil(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public Oil(int dimmension) {
		this(dimmension, dimmension);
	}

	public Oil() {
		this(3);
	}

	@Override
	protected int getNewPixelVal(TiledImage src, Point p, int channel) {
		int[] shist = new int[256];

		Point pIter = new Point();
		for (pIter.y = 0; pIter.y < windowHeight; pIter.y++)
			for (pIter.x = 0; pIter.x < windowWidth; pIter.x++) {
				int val = getWindowPixelVal(src, pIter, channel);
				shist[val]++; // localhistogram
			}

		int mostindex = 0;
		int count = shist[0]; // we look for a peak (first peak? last peak?
		// this a good question)
		for (int i = 0; i < shist.length; i++) {
			if (count < shist[i]) {
				mostindex = i;
				count = shist[i];
			}
		}

		return mostindex;
	}

	@Override
	protected void setWindowProperties() {
		windowWidth = width;
		windowHeight = height;
		dstPointInWindow = new Point(width / 2, height / 2);
	}
}